package ar.com.solidussource.howmuchmobile.maps.overlays;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import ar.com.solidussource.howmuchmobile.activities.MarketClusterListActivity;
import ar.com.solidussource.howmuchmobile.maps.items.MarketClusterItem;
import com.google.android.maps.ItemizedOverlay;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: FT31270775
 * Date: 12/11/12
 * Time: 16:10
 * To change this template use File | Settings | File Templates.
 */
public class MarketClusterItemizedOverlay extends ItemizedOverlay<MarketClusterItem> {

    private List<MarketClusterItem> mOverlays = new ArrayList<MarketClusterItem>();
    private Activity mContext;

    public MarketClusterItemizedOverlay(Drawable drawable) {
        super(boundCenterBottom(drawable));
    }

    public MarketClusterItemizedOverlay(Drawable defaultMarker, Activity context) {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
        populate();
    }

    @Override
    protected MarketClusterItem createItem(int i) {
        return mOverlays.get(i);
    }

    @Override
    public int size() {
        return mOverlays.size();
    }

    public synchronized void addOverlay(MarketClusterItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    @Override
    protected boolean onTap(int index) {
        MarketClusterItem item = mOverlays.get(index);

        Intent intent = new Intent(mContext, MarketClusterListActivity.class);
        intent.putExtra("marketCluster", item.getMarketCluster());
        mContext.startActivity(intent);

        return true;
    }
}